package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"strconv"
)

type Riff2 struct {
	id   []byte
	size []byte
	typ  []byte
}
type Format2 struct {
	id            []byte
	size          []byte
	audioFormat   []byte
	numChannels   []byte
	sampleRate    []byte
	byteRate      []byte
	blockAlign    []byte
	bitsPerSample []byte
}
type WavData2 struct {
	id   []byte
	size []byte
	data []byte
}
type WavFile2 struct {
	riff    Riff2
	format  Format2
	wavData WavData2
}

func main() {

	f, err := os.Open("b_page_1.wav")
	if err != nil {
		fmt.Println("os Open error: ", err)
		return
	}
	defer f.Close()

	//读取文件全部内容
	b, err := ioutil.ReadAll(f)
	if err != nil {
		fmt.Println("ioutil ReadAll error: ", err)
		return
	}
	var riffOld Riff2
	riffOld.id = b[:4]
	riffOld.size = b[4:8]
	riffOld.typ = b[8:12]
	//fmt.Println(len(b))
	//fmt.Println(b[:4])//'RIFF' (0x52494646)大端
	//fmt.Println(BinaryStringToInt(ListToBinaryString(b[:4])))//46464952大端反了
	//fmt.Println(b[4:8])//fileSize - 8小端
	//fmt.Println(BinaryStringToInt(ListToBinaryString(b[4:8])))
	//724510-8=724502
	//fmt.Println(b[8:12])//'WAVE'(0x57415645)
	//fmt.Println()
	var formatOld Format2
	formatOld.id = b[12:16]
	formatOld.size = b[16:20]
	formatOld.audioFormat = b[20:22]
	formatOld.numChannels = b[22:24]
	formatOld.sampleRate = b[24:28]
	formatOld.byteRate = b[28:32]
	formatOld.blockAlign = b[32:34]
	formatOld.bitsPerSample = b[34:36]
	//fmt.Println(b[12:16])//'fmt ' (0x666D7420)
	//fmt.Println(b[16:20])//16小端
	//fmt.Println(b[20:22])//音频格式
	fmt.Println(BinaryStringToInt(ListToBinaryString(b[22:24]))) //声道数
	fmt.Println(BinaryStringToInt(ListToBinaryString(b[24:28]))) //采样率
	//fmt.Println(b[28:32])//每秒数据字节数
	//fmt.Println(b[32:34])//数据块对齐
	fmt.Println(BinaryStringToInt(ListToBinaryString(b[34:36]))) //采样位数
	//fmt.Println()
	var wavDatOld WavData2
	wavDatOld.id = b[36:40]
	wavDatOld.size = b[40:44]
	wavDatOld.data = b[44:]
	var old WavFile2
	old.format = formatOld
	old.riff = riffOld
	old.wavData = wavDatOld
	//fmt.Println(b[36:40])//'data' (0x64617461)
	fmt.Println(b[40:44])    //N小端
	fmt.Println(len(b[44:])) //音频数据
	//724466
	fmt.Println(intToByte(724466))
	dat := b[44:]
	inlen := BinaryStringToInt(ListToBinaryString(b[40:44]))
	outlen := BinaryStringToInt(ListToBinaryString(b[4:8]))
	poNuM := len(dat) / 2
	modNum := poNuM % 2
	//对于16位单声道，每个样本数据由16位（bit）表示；其中低字节
	if modNum == 1 {
		poNuM = poNuM + 1
	}
	//fmt.Println(len(dat)/2+1)
	dat1 := dat[:poNuM]
	dat2 := dat[poNuM:]
	inlen1 := len(dat1)
	inlen2 := len(dat2)
	outlen1 := inlen1 + (outlen - inlen)
	outlen2 := inlen2 + (outlen - inlen)

	var newWav1 WavFile2
	newWav1 = deepCopy(old)
	var newWav2 WavFile2
	newWav2 = deepCopy(old)
	newWav1.wavData.data = dat1
	newWav2.wavData.data = dat2
	newWav1.wavData.size = intToByte(inlen1)
	newWav2.wavData.size = intToByte(inlen2)
	newWav1.riff.size = intToByte(outlen1)
	newWav2.riff.size = intToByte(outlen2)

	//int string slice int[]
	//fmt.Println(ToBinaryString(inlen1))
	con1 := ObjToByte2(newWav1)
	con2 := ObjToByte2(newWav2)
	//content1=b[:len(b)/2]
	//content2:=b[len(b)/2:]
	////将指定内容写入到文件中
	err = ioutil.WriteFile("output1.wav", con1, 0666)
	if err != nil {
		fmt.Println("ioutil WriteFile error: ", err)
	}
	err = ioutil.WriteFile("output2.wav", con2, 0666)
	if err != nil {
		fmt.Println("ioutil WriteFile error: ", err)
	}
}

//int转byte[]4*8=32
func ObjToByte2(val WavFile2) (res []byte) {
	res = append(res, val.riff.id...)
	res = append(res, val.riff.size...)
	res = append(res, val.riff.typ...)

	res = append(res, val.format.id...)
	res = append(res, val.format.size...)
	res = append(res, val.format.audioFormat...)
	res = append(res, val.format.numChannels...)
	res = append(res, val.format.sampleRate...)
	res = append(res, val.format.byteRate...)
	res = append(res, val.format.blockAlign...)
	res = append(res, val.format.bitsPerSample...)

	res = append(res, val.wavData.id...)
	res = append(res, val.wavData.size...)
	res = append(res, val.wavData.data...)

	return
}

//int转byte[]4*8=32
func intToByte2(val int) (res []byte) {
	str := ToBinaryString(val)
	for i := len(str); i < 32; i++ {
		str = "0" + str
	}
	var tempInt [4]int
	tempInt[0] = BinaryStringToInt(str[24:32])
	tempInt[1] = BinaryStringToInt(str[16:24])
	tempInt[2] = BinaryStringToInt(str[8:16])
	tempInt[3] = BinaryStringToInt(str[:8])

	res = make([]byte, 4)
	for index := 0; index < 4; index++ {
		res[index] = byte(tempInt[index])
	}
	return
}

//byte[]转字符串
func ListToBinaryString2(b []byte) (str string) {
	str = ""
	for i := 0; i < len(b); i++ {
		str = ToBinaryString(int(b[i])) + str
	}
	return
}

//int转字节字符串
func ToBinaryString2(val int) (str string) {
	str = ""
	for val/2 > 0 {
		res := val % 2
		str = "" + strconv.Itoa(res) + str
		val = val / 2
	}
	str = "" + strconv.Itoa(val) + str
	for i := len(str); i < 8; i++ {
		str = "0" + str
	}
	return
}

//string转int
func BinaryStringToInt2(str string) (res int) {
	res = 0
	sum := 1
	for i := len(str); i > 0; i-- {
		num, _ := strconv.Atoi(str[i-1 : i])
		res = res + sum*num
		sum = sum * 2
	}
	return
}

//深拷贝
func deepCopy2(src WavFile2) WavFile2 {

	var wavDatOld WavData2
	wavDatOld.id = src.wavData.id
	wavDatOld.size = src.wavData.size
	wavDatOld.data = src.wavData.data

	var formatOld Format2
	formatOld.id = src.format.id
	formatOld.size = src.format.size
	formatOld.audioFormat = src.format.audioFormat
	formatOld.numChannels = src.format.numChannels
	formatOld.sampleRate = src.format.sampleRate
	formatOld.byteRate = src.format.byteRate
	formatOld.blockAlign = src.format.blockAlign
	formatOld.bitsPerSample = src.format.bitsPerSample

	var riffOld Riff2
	riffOld.id = src.riff.id
	riffOld.size = src.riff.size
	riffOld.typ = src.riff.typ

	var dst WavFile2
	dst.format = formatOld
	dst.riff = riffOld
	dst.wavData = wavDatOld

	return dst
}
